<!--
 - MineAdmin is committed to providing solutions for quickly building web applications
 - Please view the LICENSE file that was distributed with this source code,
 - For the full copyright and license information.
 - Thank you very much for using MineAdmin.
 -
 - @Author X.Mo<root@imoi.cn>
 - @Link   https://gitee.com/xmo/mineadmin-vue
-->
<template>
    <a-drawer v-model:visible="visible" width="80%" :footer="false">
      <template #title>创建供应商账单</template>
      <!-- CRUD 组件 -->
      <ma-crud :options="crud" :columns="columns" ref="crudRef">
        <template #operationCell="{ record }">   

            <a-link
            type="primary"
            @click="createBill(record)"
            v-if="record.supplier"
        >
          <icon-edit /> {{ '预览'}}
        </a-link>
        <span v-else>供应商不存在</span>
        </template>
        <template #tableButtons>
            <slot name="tableButtons" >
              <a-button v-for="button in buttonGroups"
              @click="createAll()" type="primary"
              class="w-full lg:w-auto mt-2 lg:mt-0"
                >
                勾选生成
                </a-button>
            </slot>
          </template>

      </ma-crud>
      <batch-action-modal v-model="batch.visible"
      :crud-ref="crudRef"
      :selected="selected"
      ref="batchActionRef"
      ></batch-action-modal>
    </a-drawer>

  </template>
  
  <script setup>
  import { ref, reactive ,computed,inject} from 'vue'
  import suppliersBill from '@/api/finance/suppliersBill'
  import { Message } from '@arco-design/web-vue'
  import BatchActionModal from "@cps/actions/batchActionModal.vue"
  import dicthelper from "@/utils/dicthelp.js";
  const emit = defineEmits(["child-event"])
  const batchActionRef = ref()
  const batch = ref({visible:false,})
  const selected = computed(()=>crudRef.value?.selecteds)
  const crudRef = ref()
  const parentCrudRef = inject('crudRef')
  const visible = ref(false)
  const buttonGroups = [
            {title:'勾选生成',displayDataIndex:"id",},
    ]
  const createBill = (record) => {
        record.bill_data = crudRef.value.requestParams.bill_data
        record.bill_type = 1
        emit('child-event',record)
    }
  const createAll = () => {
        if (!selected || selected.length == 0) {
            Message.error('至少选择一条数据')
            return;
        }
        batchActionRef.value.open({
          title:'创建客户账单',
          api:suppliersBill.save,
          displayDataIndex:"ids",
          form:{
            id:'ids',
            submited:()=>{
              parentCrudRef.value.refresh()
            }
          }
        })
    }
  const requestData = () => {
    crudRef.value.requestData()
  }

  const open = () => {
    requestData()
    visible.value = true
  }
  const getCurrentDate = ()=> {
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      return  [`${year-1}-${month}-${day}`,`${year}-${month}-${day}`];
    }
    
  const crud = reactive({
    autoRequest: false,
    api: suppliersBill.getFeeList,
    showIndex: false,
    pk: 'ids',
    operationColumn: true,
    operationColumnWidth: 80,
    beforeRequest: (params) => {
        if(!params.bill_data){
            params.bill_data = getCurrentDate()
        }
  },
    pageLayout: 'fixed',
    formOption: {
        viewType: 'drawer',
        width: "60%",
    },

    rowSelection: { showCheckedAll: true },
        add: {
        show: true,
        api: suppliersBill.save,
        auth: ['finance:suppliersBill:save']
    },
  })
  
  const columns = reactive([
    {
        title: "供应商",
        dataIndex: "user_id",
        formType: "select",
        disabled:true,
        dict: dicthelper.supplier,
        search: true
    },
    {
        title: "币种",
        dataIndex: "currency",
        formType: "select",
        dict: {
          name: "currencyType",
          translation: true
        },
        search: true
    },
    {
        title: "业务时间",
        dataIndex: "bill_data",
        formType: "range",
        searchDefaultValue:getCurrentDate(),
        disabled:true,
        search:true,
        hide:true,
    },
    {
        title: "待审计",
        dataIndex: "unaudit",
        addDisplay:false,
        editDisplay:false,
    },
    {
        title: "已审计",
        dataIndex: "audit",
        addDisplay:false,
        editDisplay:false,
    },
    {
        title: "总欠款",
        dataIndex: "amount",
        addDisplay:false,
        editDisplay:false,
    },
  ])
  
  defineExpose({ open,requestData})
  </script>
  
  <style scoped>
  </style>